home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 1.iso
/
toolbox
/
src
/
demos
/
OpenGL
/
tex_cube
/
tex_cube.c++
< prev
next >
Wrap
C/C++ Source or Header
|
1996-11-11
|
17KB
|
642 lines
/*
* (c) Copyright 1993, 1994, Silicon Graphics, Inc.
* ALL RIGHTS RESERVED
* Permission to use, copy, modify, and distribute this software for
* any purpose and without fee is hereby granted, provided that the above
* copyright notice appear in all copies and that both the copyright notice
* and this permission notice appear in supporting documentation, and that
* the name of Silicon Graphics, Inc. not be used in advertising
* or publicity pertaining to distribution of the software without specific,
* written prior permission.
*
* THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
* AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
* FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
* GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
* SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
* KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
* LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
* THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
* ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
* POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
*
* US Government Users Restricted Rights
* Use, duplication, or disclosure by the Government is subject to
* restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
* (c)(1)(ii) of the Rights in Technical Data and Computer Software
* clause at DFARS 252.227-7013 and/or in similar or successor
* clauses in the FAR or the DOD or NASA FAR Supplement.
* Unpublished-- rights reserved under the copyright laws of the
* United States. Contractor/manufacturer is Silicon Graphics,
* Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
*
* OpenGL(TM) is a trademark of Silicon Graphics, Inc.
*/
#include "oglwindow.h"
#include "unitsquare.h"
#include "texture.h"
#include <X11/keysym.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <sys/time.h>
oglwindow *window;
unitsquare *square;
texture **texmap;
GLfloat matrix[16];
// Texture map to use by default - very hard-coded
static char default_image[] = "flowers2.rgb";
// Font for the initial wait message
const char default_font[] = "-*-helvetica-bold-r-*-*-17-*-*-*-*-*-*-*";
int use_texture = 1; // Use textures?
int n_textures = 1; // Number of textures
int reload_textures = 0; // Reload textures?
int cube = 1; // Draw cube instead of square?
int transparency = 0; // Has transparency --> multipass?
int immediate = 0; // Immediate mode?
float rotx = 0.0, roty = 0.0; // Rotations about x and y per second
int paused = 0;
int win_width, win_height;
const float mouse_poll = 100000; // How often to poll mouse
static int list_names[] = {1, 2, 3, 4, 5, 6};
void draw_square();
void define_cube();
void draw_cube();
inline unsigned long current_time()
{
struct timeval time;
gettimeofday(&time, NULL);
return (time.tv_sec * 1000000 + time.tv_usec);
}
void draw_square()
{
glPushMatrix();
if (reload_textures) texmap[1 % n_textures]->specify_texture();
if (!use_texture) glColor3ub(255, 255, 255);
square->draw();
glPopMatrix();
}
void define_cube()
{
glNewList(1, GL_COMPILE);
glPushMatrix();
glTranslatef(0, 0, 1.0);
if (reload_textures) texmap[1 % n_textures]->specify_texture();
if (!use_texture) glColor3ub(0, 0, 255);
square->draw();
glPopMatrix();
glEndList();
glNewList(2, GL_COMPILE);
glPushMatrix();
glRotatef(180, 1, 0, 0);
glTranslatef(0, 0, 1.0);
if (reload_textures) texmap[6 % n_textures]->specify_texture();
if (!use_texture) glColor3ub(0, 0, 255);
square->draw();
glPopMatrix();
glEndList();
glNewList(3, GL_COMPILE);
glPushMatrix();
glRotatef(90, 0, 1.0, 0);
glTranslatef(0, 0, 1.0);
if (reload_textures) texmap[2 % n_textures]->specify_texture();
if (!use_texture) glColor3ub(255, 0, 0);
square->draw();
glPopMatrix();
glEndList();
glNewList(4, GL_COMPILE);
glPushMatrix();
glRotatef(-90, 0, 1.0, 0);
glTranslatef(0, 0, 1.0);
if (reload_textures) texmap[5 % n_textures]->specify_texture();
if (!use_texture) glColor3ub(255, 0, 0);
square->draw();
glPopMatrix();
glEndList();
glNewList(5, GL_COMPILE);
glPushMatrix();
glRotatef(90, 1.0, 0.0, 0);
glTranslatef(0, 0, 1.0);
if (reload_textures) texmap[3 % n_textures]->specify_texture();
if (!use_texture) glColor3ub(0, 255, 0);
square->draw();
glPopMatrix();
glEndList();
glNewList(6, GL_COMPILE);
glPushMatrix();
glRotatef(-90, 1.0, 0.0, 0);
glTranslatef(0, 0, 1.0);
if (reload_textures) texmap[4 % n_textures]->specify_texture();
if (!use_texture) glColor3ub(0, 255, 0);
square->draw();
glPopMatrix();
glEndList();
}
void call_cube()
{
glCallLists(6, GL_INT, list_names);
}
void draw_cube()
{
{
glPushMatrix();
glTranslatef(0, 0, 1.0);
if (reload_textures) texmap[1 % n_textures]->specify_texture();
if (!use_texture) glColor3ub(0, 0, 255);
square->draw();
glPopMatrix();
}
{
glPushMatrix();
glRotatef(180, 1, 0, 0);
glTranslatef(0, 0, 1.0);
if (reload_textures) texmap[6 % n_textures]->specify_texture();
if (!use_texture) glColor3ub(0, 0, 255);
square->draw();
glPopMatrix();
}
{
glPushMatrix();
glRotatef(90, 0, 1.0, 0);
glTranslatef(0, 0, 1.0);
if (reload_textures) texmap[2 % n_textures]->specify_texture();
if (!use_texture) glColor3ub(255, 0, 0);
square->draw();
glPopMatrix();
}
{
glPushMatrix();
glRotatef(-90, 0, 1.0, 0);
glTranslatef(0, 0, 1.0);
if (reload_textures) texmap[5 % n_textures]->specify_texture();
if (!use_texture) glColor3ub(255, 0, 0);
square->draw();
glPopMatrix();
}
{
glPushMatrix();
glRotatef(90, 1.0, 0.0, 0);
glTranslatef(0, 0, 1.0);
if (reload_textures) texmap[3 % n_textures]->specify_texture();
if (!use_texture) glColor3ub(0, 255, 0);
square->draw();
glPopMatrix();
}
{
glPushMatrix();
glRotatef(-90, 1.0, 0.0, 0);
glTranslatef(0, 0, 1.0);
if (reload_textures) texmap[4 % n_textures]->specify_texture();
if (!use_texture) glColor3ub(0, 255, 0);
square->draw();
glPopMatrix();
}
}
void do_rotation(unsigned long last_time, unsigned long time)
{
float elapsed_time;
elapsed_time = (time - last_time) / 1000000.0;
glPushMatrix();
glLoadIdentity();
if (!paused) {
glRotatef(elapsed_time * rotx, 1, 0, 0);
glRotatef(elapsed_time * roty, 0, 1, 0);
}
glMultMatrixf(matrix);
glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
glPopMatrix();
glMultMatrixf(matrix);
}
unsigned long draw(unsigned long last_draw)
{
unsigned long time = current_time();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective (45.0, 1.0, 0.01, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0, 0, -5);
glClear(GL_COLOR_BUFFER_BIT);
glColor4ub(255, 255, 255, 255);
if (use_texture) glEnable(GL_TEXTURE_2D);
glPushMatrix();
do_rotation(last_draw, time);
{
if (transparency) {
glFrontFace(GL_CW);
if (cube)
if (immediate) draw_cube();
else call_cube();
else draw_square();
glFrontFace(GL_CCW);
}
if (cube)
if (immediate) draw_cube();
else call_cube();
else draw_square();
}
glPopMatrix();
window->swapbuffers();
return time;
}
int mouse_motion(int oldmx, int oldmy, unsigned long last_time,
int mx, int my, unsigned long time)
{
float dx, dy, dt;
dt = (time - last_time) / 1000000.0;
// Find the mouse motion vector in screen space
dx = (float)(mx - oldmx) / (float)win_width;
dy = (float)(my - oldmy) / (float)win_height;
rotx = dy * 180.0 / dt;
roty = dx * 180.0 / dt;
if (rotx || roty) return 1;
else return 0;
}
void draw_text(char *string, XFontStruct *f, GLint list)
{
int i, n = strlen(string), offset = list - f->min_char_or_byte2;
for (i = 0; i < n; i++) {
glCallList(string[i] + offset);
glTranslatef(f->max_bounds.width, 0, 0);
}
}
void draw_initscreen(Display *dpy, int width, int height)
{
XFontStruct *f;
glClear(GL_COLOR_BUFFER_BIT);
f = XLoadQueryFont(dpy, (char *)default_font);
if (f != NULL) {
glXUseXFont(f->fid, f->min_char_or_byte2,
f->max_char_or_byte2 - f->min_char_or_byte2, 1);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glScalef(2. / (float)width, 2. / (float)height, 1);
glRasterPos2i(-width / 2 + f->max_bounds.width,
(height / 2) - 2. * (f->max_bounds.ascent));
draw_text("Loading textures...", f, 1);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
} else printf("Loading textures...\n");
window->swapbuffers();
if (f != NULL) {
glDeleteLists(1, f->max_char_or_byte2 - f->min_char_or_byte2);
XUnloadFont(dpy, f->fid);
}
}
void main(int argc, char **argv)
{
Display *dpy;
Window winid;
XEvent event;
char buffer[5];
int bufsize = 5;
KeySym key;
XComposeStatus compose;
int fname;
GLenum display_type = DEFAULT_DISPLAY_TYPE;
GLenum display_format = DEFAULT_DISPLAY_FORMAT;
int alignment = DEFAULT_ALIGNMENT;
int usign = 0;
int components = DEFAULT_COMPONENTS;
int level = DEFAULT_LEVEL;
GLenum min_filter = GL_LINEAR_MIPMAP_LINEAR;
GLenum max_filter = GL_LINEAR;
GLenum environment = DEFAULT_ENVIRONMENT;
int map_to_alpha = 1;
int clamp = 1;
int isize = 512;
int moving = 1;
int mx, my, oldmx, oldmy;
unsigned long time, last_mouse_time, last_draw = 0.0;
int button_down = 0;
Window root_return, child_return;
int root_x, root_y;
unsigned int mask_return;
int i, j;
window = new oglwindow();
square = new unitsquare();
window->set_title("textured cube");
window->add_event_mask(ButtonReleaseMask);
window->set_doublebuffer(1);
window->open();
square->open();
fname = argc;
for (i = 1; i < argc; i++) {
if (argv[i][0] != '-') {
fname = i;
break;
}
switch(argv[i][1]) {
case 'a': // Alignment
alignment = atoi(&argv[i][2]);
break;
case 'C': // Clamping
clamp = atoi(&argv[i][2]);
break;
case 'c': // Components
components = atoi(&argv[i][2]);
break;
case 'e': // Texture environment
switch(argv[i][2]) {
case 'm':
environment = GL_MODULATE;
break;
case 'd':
environment = GL_DECAL;
break;
case 'b':
environment = GL_BLEND;
break;
}
break;
case 'f': // Format
if (!strcmp(argv[i]+2, "r")) display_format = GL_RED;
else if (!strcmp(argv[i]+2, "g")) display_format = GL_GREEN;
else if (!strcmp(argv[i]+2, "b")) display_format = GL_BLUE;
else if (!strcmp(argv[i]+2, "a")) display_format = GL_ALPHA;
else if (!strcmp(argv[i]+2, "rgb")) display_format = GL_RGB;
else if (!strcmp(argv[i]+2, "rgba")) display_format = GL_RGBA;
else if (!strcmp(argv[i]+2, "l")) display_format = GL_LUMINANCE;
else if (!strcmp(argv[i]+2, "la")) display_format =
GL_LUMINANCE_ALPHA;
else fprintf(stderr, "Unrecognized display format option %s.\n",
argv[i]);
break;
case 'I': // Immediate Mode?
immediate = atoi(argv[i]+2);
break;
case 'i': // Minimize filter
if (!strcmp(argv[i]+2, "n")) min_filter = GL_NEAREST;
else if (!strcmp(argv[i]+2, "l")) min_filter = GL_LINEAR;
else if (!strcmp(argv[i]+2, "nmn")) min_filter =
GL_NEAREST_MIPMAP_NEAREST;
else if (!strcmp(argv[i]+2, "lmn")) min_filter =
GL_LINEAR_MIPMAP_NEAREST;
else if (!strcmp(argv[i]+2, "nml")) min_filter =
GL_NEAREST_MIPMAP_LINEAR;
else if (!strcmp(argv[i]+2, "lml")) min_filter =
GL_LINEAR_MIPMAP_LINEAR;
else fprintf(stderr, "Unrecognized minimize filter: %s.\n", argv[i]);
break;
case 'l': // Mipmap reduction level
level = atoi(&argv[i][2]);
break;
case 'm': // Map luminance to alpha
map_to_alpha = atoi(&argv[i][2]);
break;
case 'n': // Number of textures
n_textures = atoi(&argv[i][2]);
if (n_textures > 1) reload_textures = 1;
break;
case 'r': // Reload textures
if (argv[i][2] == '0') reload_textures = 0;
else reload_textures = 1;
break;
case 'S': // Size of image
isize = atoi(&argv[i][2]);
break;
case 't': // Texture map at all?
if (argv[i][2] == '0') use_texture = 0;
else use_texture = 1;
break;
case 'u':
cube = atoi(argv[i]+2);
break;
case 'x': // Maximize filter
if (!strcmp(argv[i]+2, "n")) max_filter = GL_NEAREST;
else if (!strcmp(argv[i]+2, "l")) max_filter = GL_LINEAR;
else fprintf(stderr, "Unrecognized maximize filter: %s.\n", argv[i]);
break;
case 'y': // Display type;
if (argv[i][2] == 'u') {
usign = 1;
argv[i]++;
}
switch(argv[i][2]) {
case 'b':
display_type = usign ? GL_UNSIGNED_BYTE : GL_BYTE;
break;
case 's':
display_type = usign ? GL_UNSIGNED_SHORT : GL_SHORT;
break;
case 'i':
display_type = usign ? GL_UNSIGNED_INT : GL_INT;
break;
default:
fprintf(stderr, "Unrecognized display type option %s.\n",
argv[i]);
break;
}
break;
case 'h':
default:
fprintf(stderr, "Usage:\n");
fprintf(stderr, "tex_test [-t{0}] [file1.rgb file2.rgb...]\n");
exit(1);
}
}
window->map();
window->winset();
dpy = window->get_display();
winid = window->get_window();
win_width = window->get_width();
win_height = window->get_height();
draw_initscreen(dpy, win_width, win_height);
if (use_texture) {
if (argc - fname > n_textures) n_textures = argc - fname;
if (argc - fname == 0) {
fname = argc - 1;
argv[fname] = default_image;
}
texmap = (texture **)malloc(n_textures * sizeof(texture *));
for (i = 0; i < n_textures; i++) {
texmap[i] = new texture();
texmap[i]->open();
if (fname < argc) texmap[i]->create_from_file(argv[fname++]);
else
if (i % 2)
texmap[i]->create_diag_stripes(isize, (int)(pow(2.0, 6.0 - 1.0)),
1.0 * (float)((i+1)%2),
0.5 * (float)((i+1)%3),
.33 * (float)((i+1)%4), 1.0, 1.0,
.33 * (float)(i%4),
0.5 * (float)(i%3),
1.0 * (float)(i%2), 1.0, 1.0);
else
texmap[i]->create_checkerboard(isize, (int)(pow(2.0, 6.0)),
1.0 * (float)((i+1)%2),
0.5 * (float)((i+1)%3),
.33 * (float)((i+1)%4), 1.0, 1.0,
.33 * (float)((i+1)%4),
0.5 * (float)((i+1)%3),
1.0 * (float)((i+1)%2), 1.0, 1.0);
if (map_to_alpha) texmap[i]->map_lum_to_alpha();
}
}
// Now do everything that requires a graphics context
moving = 0;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
if (use_texture) {
for (i = 0; i < n_textures; i++) {
texmap[i]->set_display_type(display_type);
texmap[i]->set_display_format(display_format);
texmap[i]->set_alignment(alignment);
texmap[i]->set_components(components);
texmap[i]->set_level(level);
texmap[i]->set_min_filter(min_filter);
texmap[i]->set_max_filter(max_filter);
texmap[i]->set_environment(environment);
}
texmap[0]->specify_texture();
}
if (map_to_alpha && use_texture) {
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
transparency = 1;
}
glEnable(GL_CULL_FACE);
if (clamp) {
j = GL_CLAMP;
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
}
if (!immediate) {
define_cube();
if (use_texture)
for (i = 0; i < n_textures; i++) texmap[i]->pixels_free();
}
while (1) {
while(((moving || button_down) && XPending(dpy)) ||
(moving == 0 && button_down == 0)) {
XNextEvent(dpy, &event);
switch(event.type) {
case Expose:
last_draw = draw(last_draw);
break;
case ConfigureNotify:
window->resize();
win_width = window->get_width();
win_height = window->get_height();
break;
case ButtonPress:
button_down = 1;
oldmx = event.xbutton.x;
oldmy = event.xbutton.y;
last_mouse_time = current_time();
break;
case ButtonRelease:
button_down = 0;
break;
case KeyPress:
XLookupString(&event.xkey, buffer, bufsize, &key, &compose);
if (key == XK_Escape) exit(0);
else if (key == XK_h || key == XK_H) {
rotx = roty = 0.0;
moving = 0;
paused = 0;
last_draw = draw(last_draw);
}
else if (key == XK_w || key == XK_W) {
rotx = roty = 0.0;
glPushMatrix();
glLoadIdentity();
glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
glPopMatrix();
moving = 0;
paused = 0;
last_draw = draw(last_draw);
}
else if (key == XK_space) paused = ~paused;
break;
}
}
if (moving) time = last_draw = draw(last_draw);
else time = current_time();
if (button_down && time - last_mouse_time > mouse_poll) {
XQueryPointer(dpy, winid, &root_return, &child_return, &root_x,
&root_y, &mx, &my, &mask_return);
moving = mouse_motion(oldmx, oldmy, last_mouse_time, mx, my, time);
oldmx = mx;
oldmy = my;
last_mouse_time = time;
}
}
}